#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Premiere.DTO;
using Premiere.Window;
using Premiere.Framework;

#endregion

namespace PremiereMX
{
    public partial class MarkDownSellingPriceForm : Form
    {
        IList m_partgroupList;
        IList m_partCategoryList1;
        IList m_partCategoryList2;
        IList m_partCategoryList3;
        IList m_priceCategoryList;
        IList m_partList;
        IList m_currencyList;
        const string ALL = "ALL";
        PartGroupDTO m_allPartGroup = new PartGroupDTO(0, ALL, "<<ALL PART GROUP>>");
        PartDTO m_allPart = new PartDTO(0, ALL, "<<ALL PART>>");
        MarkUpDownSellingPriceDTO m_subject = new MarkUpDownSellingPriceDTO();
        IMainForm m_mainForm;
        public MarkDownSellingPriceForm(PartDTO part, IMainForm mainForm)
        {
            InitializeComponent();
            loadComboDataSource();
            m_mainForm = mainForm;
            partComboBox.Text = part.Id == 0 ? m_allPart.ToString() : part.ToString();
            partGroupComboBox.Text = part.PartGroup == null ? m_allPartGroup.ToString() : part.PartGroup.ToString();
            partCategory1ComboBox.Text = part.PartCategory1 == null ? string.Empty : part.PartCategory1.ToString();
            partCategory2ComboBox.Text = part.PartCategory2 == null ? string.Empty : part.PartCategory2.ToString();
            partCategory3ComboBox.Text = part.PartCategory3 == null ? string.Empty : part.PartCategory3.ToString();

            bool categoryEnable = m_mainForm.GetGeneralSetup().PartCategory;
            partCategory1ComboBox.Enabled = m_mainForm.GetGeneralSetup().PartCategory1 && categoryEnable;
            partCategory2ComboBox.Enabled = m_mainForm.GetGeneralSetup().PartCategory2 && categoryEnable;
            partCategory3ComboBox.Enabled = m_mainForm.GetGeneralSetup().PartCategory3 && categoryEnable;

            labelPartCategory1.Text = m_mainForm.GetGeneralSetup().PartCategory1ColumnName;
            labelPartCategory2.Text = m_mainForm.GetGeneralSetup().PartCategory2ColumnName;
            labelPartCategory3.Text = m_mainForm.GetGeneralSetup().PartCategory3ColumnName;
        }
        private void loadComboDataSource()
        {
            m_partgroupList = ServiceRegistry.GetInstance().GetPartGroupService().FindAllIdCodeName();
            m_partgroupList.Add(m_allPartGroup);
            m_partCategoryList1 = ServiceRegistry.GetInstance().GetPartCategoryService().FindAllPartCategory1();
            m_partCategoryList2 = ServiceRegistry.GetInstance().GetPartCategoryService().FindAllPartCategory2();
            m_partCategoryList3 = ServiceRegistry.GetInstance().GetPartCategoryService().FindAllPartCategory3();
            m_priceCategoryList = ServiceRegistry.GetInstance().GetPriceCategoryService().FindAllIdCodeName();
            m_partList = ServiceRegistry.GetInstance().GetPartService().FindAllIdCodeName();
            m_allPart.DTOToString = DTOToString.Name;
            m_partList.Add(m_allPart);
            m_currencyList = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();


            partGroupComboBox.DataSource = m_partgroupList;
            partCategory1ComboBox.DataSource = m_partCategoryList1;
            partCategory2ComboBox.DataSource = m_partCategoryList2;
            partCategory3ComboBox.DataSource = m_partCategoryList3;
            priceCategoryComboBox.DataSource = m_priceCategoryList;
            partComboBox.DataSource = m_partList;
            currencyComboBox.DataSource = m_currencyList;

            typeComboBox.DataSource = Enum.GetValues(typeof(MarkUpDownSellingPriceType));
            baseOnComboBox.DataSource = Enum.GetValues(typeof(MarkUpDownSellingPriceBaseOn));
            markTypeComboBox.DataSource = Enum.GetValues(typeof(MarkUpDownSellingPriceMarkType));

            partGroupComboBox.Text = string.Empty;
            partCategory1ComboBox.Text = string.Empty;
            partCategory2ComboBox.Text = string.Empty;
            partCategory3ComboBox.Text = string.Empty;
            priceCategoryComboBox.Text = string.Empty;
            partComboBox.Text = string.Empty;

            partGroupComboBox.SelectedItem = null;
            partCategory1ComboBox.SelectedItem = null;
            partCategory2ComboBox.SelectedItem = null;
            partCategory3ComboBox.SelectedItem = null;
            priceCategoryComboBox.SelectedItem = null;
            partComboBox.SelectedItem = null;

            typeComboBox.SelectedItem = MarkUpDownSellingPriceType.SellingPrice;
        }
        private void updateSubject()
        {
            m_subject.ComputerName = Environment.MachineName;
            m_subject.Currency = currencyComboBox.SelectedItem as CurrencyDTO;
            m_subject.LastUpdate = DateTime.Today;
            m_subject.MarkBaseOn = (MarkUpDownSellingPriceBaseOn)baseOnComboBox.SelectedItem;
            m_subject.MarkMarkType = (MarkUpDownSellingPriceMarkType)markTypeComboBox.SelectedItem;
            m_subject.MarkType = (MarkUpDownSellingPriceType)typeComboBox.SelectedItem;
            m_subject.Part = ((PartDTO)partComboBox.SelectedItem).Code.Equals(ALL) ? null : partComboBox.SelectedItem as PartDTO;
            m_subject.PartCategory1 = partCategory1ComboBox.SelectedItem as PartCategoryDTO;
            m_subject.PartCategory2 = partCategory2ComboBox.SelectedItem as PartCategoryDTO;
            m_subject.PartCategory3 = partCategory3ComboBox.SelectedItem as PartCategoryDTO;
            m_subject.PartGroup = ((PartGroupDTO)partGroupComboBox.SelectedItem).Code.Equals(ALL) ? null : partGroupComboBox.SelectedItem as PartGroupDTO;
            m_subject.Percentage = percentageNumericUpDown.Value;
            m_subject.PriceCategory = priceCategoryComboBox.SelectedItem as PriceCategoryDTO;
            m_subject.Value = valueNumericUpDown.Value;
        }
        private void wizardButton_Click(object sender, EventArgs e)
        {
            try
            {
                updateSubject();
                ServiceRegistry.GetInstance().GetPartService().ExecuteMarkUpDownPartPrice(m_subject);
                MessageBox.Show("Wizard Succeded", "Information");
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error");
            }
        }
        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void markTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MarkUpDownSellingPriceMarkType type = (MarkUpDownSellingPriceMarkType)markTypeComboBox.SelectedItem;
            percentageNumericUpDown.Enabled = type.Equals(MarkUpDownSellingPriceMarkType.Percentage);
            valueNumericUpDown.Enabled = type.Equals(MarkUpDownSellingPriceMarkType.Value);
            currencyComboBox.Enabled = type.Equals(MarkUpDownSellingPriceMarkType.Value);
        }
    }
}